/*
Copyright (C) 2012  Christoph Schnack, Berlin (Germany)

This file is part of Tribuit.

    Tribuit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Tribuit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tribuit.  If not, see <http://www.gnu.org/licenses/>.
*/

package WordTable;

import java.awt.*;
import java.awt.event.*;
import java.util.HashMap;
import java.util.Vector;
import javax.swing.*;
import GuiTools.*;
import Mainframe.*;
import Settings.*;
import Statistics.Statistics;
import Storage.*;
import WordTable.WCSettings;
import Wordcheck.Wordcheck;
import Wordcheck.WordcheckPreSelection.PreOptionPanel;

public class WordTable {
	Storage MAINVOC;
	Settings SET;
	Language LANG;
	Mainframe MAINFRAME;
	TribuitFrame wtFrame;
	Color BGCOLOR = new Color(255,126,0);
	boolean LEARNFIRST=false;
	Vector<Word> wtTestvocabulary;
	Vector<JPanel> bPanels=new Vector<JPanel>(),aPanels=new Vector<JPanel>();
	Color bgCol;
	JPanel centerPanel;
	JScrollPane jsp;
	TribuitMenu MENU;
	String sourceV;
	PreOptionPanel optsPanel;
	Statistics stats;
	String vocNameA,vocNameB,vocName;
	HashMap<String, TermClass> tcsA,tcsB;
	WCSettings wcSettings;
	
	public WordTable(Storage stor, Settings set,TribuitMenu menu,Mainframe fr,WCSettings wcSettingsArg){
		MAINVOC=stor;
		SET=set;
		LANG=SET.getLanguage();
		MAINFRAME=fr;
		MENU=menu;
		wcSettings = wcSettingsArg;
		wtTestvocabulary=wcSettings.getWords();
		vocNameA = wcSettings.getVocNameA();
		vocNameB = wcSettings.getVocNameB();
		vocName = wcSettings.getVocName();
		sourceV=vocName;
		buildWordTable();
	}

	public WordTable(Storage stor, Settings set,TribuitMenu menu,Mainframe fr,WCSettings wcSettingsArg, PreOptionPanel optsPanelArg, HashMap<String, TermClass> tA,HashMap<String, TermClass> tB){
		MAINVOC=stor;
		SET=set;
		LANG=SET.getLanguage();
		MAINFRAME=fr;
		MENU=menu;
		wcSettings = wcSettingsArg;
		wtTestvocabulary=wcSettings.getWords();
		vocNameA = wcSettings.getVocNameA();
		vocNameB = wcSettings.getVocNameB();
		vocName = wcSettings.getVocName();
		sourceV=vocName;
		optsPanel = optsPanelArg;
		tcsA = tA;
		tcsB = tB;
		wcSettings = wcSettingsArg;
		stats=MAINVOC.getStatistics();
		LEARNFIRST=true;
		buildWordTable();
	}
	
	
	public void buildWordTable(){
		wtFrame=new TribuitFrame(LANG.getMenuWordTable());
		
		Word[] words = wtTestvocabulary.toArray(new Word[]{});
		
		GridBagConstraints lgbc = new GridBagConstraints();
		lgbc.anchor = GridBagConstraints.CENTER;
		lgbc.fill = GridBagConstraints.BOTH;
		lgbc.gridy++;
		lgbc.weightx=1;
		lgbc.weighty=1;
		centerPanel = new JPanel();
		centerPanel.setLayout(new GridBagLayout());
		
		JLabel headerLabelA = new JLabel(vocNameA.toUpperCase());
		headerLabelA.setFont(new Font(Font.SERIF,Font.BOLD,25));
		JLabel headerLabelB = new JLabel(vocNameB.toUpperCase());
		headerLabelB.setFont(new Font(Font.SERIF,Font.BOLD,25));
		JPanel hlA = new JPanel();
		JPanel hlB = new JPanel();
		hlA.add(headerLabelA);
		hlB.add(headerLabelB);
		Vocabulary sourceVoc = MAINVOC.getVocabulary(sourceV);
		if(wcSettings.getTestLang2()){
			lgbc.gridx = 1;
			centerPanel.add(hlB,lgbc);
			lgbc.gridx=2;
			centerPanel.add(hlA,lgbc);
			lgbc.insets = new Insets(0,30,0,0);
			for(Word w : words){
				lgbc.gridy++;
				bPanels.add(new MeaningDisplayPanel(LANG, wcSettings, w.getMeaningVectorA(), sourceVoc.getTermclassA(w.getTermclassA()), w.getTermclassA(), w.getLection()));
				aPanels.add(new MeaningDisplayPanel(LANG, wcSettings, w.getMeaningVectorB(), sourceVoc.getTermclassB(w.getTermclassB()), w.getTermclassB(), w.getLection()));
				lgbc.gridx = 1;
				centerPanel.add(aPanels.lastElement(),lgbc);
				lgbc.gridx = 2;
				centerPanel.add(bPanels.lastElement(),lgbc);
			}
		}
		else{
			lgbc.gridx = 1;
			centerPanel.add(hlA,lgbc);
			lgbc.gridx=2;
			centerPanel.add(hlB,lgbc);
			lgbc.insets = new Insets(0,30,0,0);
			for(Word w : words){
				lgbc.gridy++;
				aPanels.add(new MeaningDisplayPanel(LANG, wcSettings, w.getMeaningVectorA(), sourceVoc.getTermclassA(w.getTermclassA()), w.getTermclassA(), w.getLection()));
				bPanels.add(new MeaningDisplayPanel(LANG, wcSettings, w.getMeaningVectorB(), sourceVoc.getTermclassB(w.getTermclassB()), w.getTermclassB(), w.getLection()));
				lgbc.gridx = 1;
				centerPanel.add(aPanels.lastElement(),lgbc);
				lgbc.gridx = 2;
				centerPanel.add(bPanels.lastElement(),lgbc);
			}
		}
		bgCol = centerPanel.getBackground();
		jsp = new JScrollPane(centerPanel);
		jsp.setPreferredSize(new Dimension(550,300));
		jsp.getVerticalScrollBar().setUnitIncrement(30);
		JButton closeBtn = new JButton(LANG.getClose());
		if(LEARNFIRST)closeBtn.setText(LANG.getStart()+" "+LANG.getMenuWordcheck());
		closeBtn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				MENU.setWordtableRunning(false);
				Vocabulary voc = MAINVOC.getVocabulary(vocName);
				if(LEARNFIRST)new Wordcheck(wcSettings.getTestLang2(),optsPanel,MAINVOC, SET, MAINFRAME,MENU, wtTestvocabulary, vocNameA, vocNameB,tcsA,voc.getCategoriesA(),tcsB,voc.getCategoriesB());
				wtFrame.dispose();
			}
		});
		JButton chgBtn = new JButton(LANG.getCover());
		chgBtn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				int l = aPanels.size();
				boolean leftBlack = (aPanels.elementAt(0).getBackground()==Color.black);
				for(int i=0;i<l;i++){
					if(leftBlack){
						aPanels.elementAt(i).setBackground(bgCol);
						bPanels.elementAt(i).setBackground(Color.black);
					}
					else{
						aPanels.elementAt(i).setBackground(Color.black);
						bPanels.elementAt(i).setBackground(bgCol);
					}
				}
			}
		});
		JButton showBtn = new JButton(LANG.getShow());
		showBtn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				int l = bPanels.size();
				for(int i=0;i<l;i++){
					aPanels.elementAt(i).setBackground(bgCol);
					bPanels.elementAt(i).setBackground(bgCol);
				}
			}
		});
		JPanel buttonPanel = new JPanel();
		buttonPanel.add(chgBtn);
		buttonPanel.add(showBtn);
		buttonPanel.add(closeBtn);
		wtFrame.getContentPane().add(BorderLayout.CENTER,jsp);
		wtFrame.getContentPane().add(BorderLayout.SOUTH,buttonPanel);
		wtFrame.setJMenuBar(new WTMenu());
		wtFrame.showUp(300,200);
		wtFrame.setResizable(true);
		wtFrame.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				MENU.setWordtableRunning(false);
				Vocabulary voc = MAINVOC.getVocabulary(vocName);
				if(LEARNFIRST)new Wordcheck(wcSettings.getTestLang2(),optsPanel,MAINVOC, SET, MAINFRAME,MENU, wtTestvocabulary, vocNameA, vocNameB,tcsA,voc.getCategoriesA(),tcsB,voc.getCategoriesB());
			}
		});
		SwingUtilities.invokeLater(new Runnable(){
			public void run(){
				jsp.getVerticalScrollBar().setValue(0);
			}
		});
	}
	
	class WTMenu extends JMenuBar{
		private boolean PRINTSETTINGSDIALOGRUNNING = false;
		WTMenu menu;
		
		public WTMenu(){
			super();
			menu = this;
			JMenu opts = new JMenu(LANG.getOptions());
			JMenuItem print = new JMenuItem(LANG.getPrint());
			print.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					if(!PRINTSETTINGSDIALOGRUNNING){
						PRINTSETTINGSDIALOGRUNNING = true;
						new PrintPresettings(MAINVOC,SET,menu,wcSettings);
					}
				}
			});
			opts.add(print);
			this.add(opts);
		}

		public void setPSDRunning(boolean arg){
			PRINTSETTINGSDIALOGRUNNING = arg;
		}
	}
}
